#! /bin/bash

# This builds all the components if the tar ball RPM for eee
# ready to be deposited at /root/eee-setup.tar.gz

# Later (when one the target machine, this can be 
# unwrapped using tar -xzf /root/eee-setup.tar.gz -C /root/
# and all the files will appear in /root/eee-setup/

kver=`uname -r`
rpmver=`echo "${kver}" | sed 's|^\(.*\)-.*|\1|'`
rpmrelease=`echo "${kver}" | sed -e 's|.*-\(.*\)$|\1|'`

# This is for when there are several releases for the same kernel version
rpmminor='.02'
rpmrelease=${rpmrelease}${rpmminor}

echo "RPM ver='${rpmver}', release='${rpmrelease}'"
#exit 1

if [ `id -u` != 0 ]; then
 echo "Must be root to build this one"
	exit 0
fi


rpmname=eee_tarball
rpmln=eee-setup
rpmdir=${rpmln}_${rpmver}-${rpmrelease}

# Build the build directory for the RPM directly
mkdir ${rpmname}-${rpmver}/
cp ./${rpmname}.spec ${rpmname}-${rpmver}/


#
# Now, gather up all the stuff we would like to have 'on the other side'
#
mkdir ${rpmdir}/

###   Add more files to the tar archive here....

# This is the acpi driver (from Asus, updated for latest kernels)
mkdir ${rpmdir}/acpi
cp ./acpi/asus_acpi_eee.ko ${rpmdir}/acpi/
cp ./acpi/install-acpi ${rpmdir}/acpi/
cp ./acpi/blacklist-asus_acpi ${rpmdir}/acpi/
cp ./acpi/upgrade-acpi ${rpmdir}/acpi/
mkdir ${rpmdir}/acpi/actions ${rpmdir}/acpi/events
cp ./acpi/events/*.conf ${rpmdir}/acpi/events/
cp ./acpi/actions/eee* ${rpmdir}/acpi/actions/
cp ./acpi/dot-hotkey-app ${rpmdir}/acpi/

# Here's the wired ethernet driver 'atl2.ko'
#mkdir ${rpmdir}/atl2
#cp ./atl2/*.ko ${rpmdir}/atl2/
#cp ./atl2/install-atl2 ${rpmdir}/atl2/
## Run using script /root/${rpmdir}/atl2/install-atl2

# These are the wireless internet kernel modules and fixes
mkdir ${rpmdir}/ath
cp ./ath/*.ko ${rpmdir}/ath/
cp ./ath/install-ath ${rpmdir}/ath/
#cp ./ath/README ${rpmdir}/ath/
cp ./ath/blacklist-ath5k ${rpmdir}/ath/
cp ./ath/wifi-radar-ath ${rpmdir}/ath/
#cp ./ath/madwifi/madwifi-patched.tar.gz ${rpmdir}/ath/

# This is the webcam kernel module
mkdir ${rpmdir}/uvc
cp ./uvc/*.ko* ${rpmdir}/uvc/
cp ./uvc/install-uvc ${rpmdir}/uvc/
cp ./uvc/luvcview ${rpmdir}/uvc/
cp ./uvc/README ${rpmdir}/uvc/

# These are related to services (eg: fix-up the system on the live machine)
cp -R services ${rpmdir}/

# These are useful repos to have
cp -R repos ${rpmdir}/

# This is the stuff to get X11 fixed up right
cp -R xorg ${rpmdir}/

# This is to get the user defaults for XFCE how I like them
mkdir ${rpmdir}/xfce
cp ./xfce/setup-xfce ${rpmdir}/xfce/
cp ./xfce/eeedora-dot-config.tar.gz ${rpmdir}/xfce/
cp ./xfce/dot-xsession ${rpmdir}/xfce/
cp ./xfce/dot-xinitrc ${rpmdir}/xfce/
cp ./xfce/sysconfig-desktop ${rpmdir}/xfce/
cp ./xfce/README ${rpmdir}/xfce/

# This is to set up the defaults for XDM 
cp -R xdm ${rpmdir}/

# This contains scripts to simplify adding extra software
cp -R addons ${rpmdir}/

# This is to set up the defaults for XDM 
mkdir ${rpmdir}/artwork
cp ./artwork/splash.xpm.gz ${rpmdir}/artwork/
cp ./artwork/splash.jpg ${rpmdir}/artwork/

# This is to set up the defaults for XDM 
mkdir ${rpmdir}/truecrypt
#cp ./truecrypt/*.ko ${rpmdir}/truecrypt/
cp ./truecrypt/truecrypt ${rpmdir}/truecrypt/
cp ./truecrypt/install-truecrypt ${rpmdir}/truecrypt/
cp ./truecrypt/sudo-truecrypt-fix ${rpmdir}/truecrypt/

# ...


# This is for the live-install stuff - and to fix up the machine after the live install
cp -R live-install ${rpmdir}/

# These are misc scripts... (fstab for instance)
cp -R misc ${rpmdir}/


# ...



###   Add more files to the tar archive ubove this line

# Clean out the .svn directories 
# ( see:   http://www.linux.org/docs/ldp/howto/Bash-Prog-Intro-HOWTO-12.html#ss12.2 )
for svn in `find  ${rpmdir} | grep "\.svn\$"`; do rm -rf $svn ;done

# Too late - it's going to get built...
# Now tar it up - and put the tar in the RPM directory
tar -czf ${rpmname}-${rpmver}/eee-setup.tar.gz ${rpmdir}/
# List it all out for debugging purposes
echo "---GOING INTO THE TARBALL ----  START"
tar -tzf ${rpmname}-${rpmver}/eee-setup.tar.gz | sort | tee eee_tarball.listing
echo "---GOING INTO THE TARBALL ----  END"
rm -rf ${rpmdir}

echo "Now adjust the spec file to match the version numbers"
spec=${rpmname}-${rpmver}/${rpmname}.spec
sed -i \
  -e 's|^Version\:.*$|Version: '${rpmver}'|' \
		-e 's|^Release\:.*$|Release: '${rpmrelease}'|' \
		-e 's|^Source0\:.*$|Source0: '${rpmname}'-'${rpmver}'.tar.gz|' \
  ${spec}

echo "Make the RPM build directory into an RPM - tar up sources"
tar -czf ${rpmname}-${rpmver}.tar.gz ${rpmname}-${rpmver}
echo "Make the RPM build directory into an RPM - build rpm"
rpmbuild -tb ${rpmname}-${rpmver}.tar.gz 

echo "Cleanup"
rm -f ${rpmname}-${rpmver}.tar.gz
rm -rf ${rpmname}-${rpmver}

cp /usr/src/redhat/RPMS/i386/${rpmname}-${rpmver}-${rpmrelease}.i386.rpm .
cp /usr/src/redhat/RPMS/i386/${rpmname}-${rpmver}-${rpmrelease}.i386.rpm ../rpms-for-eee/i386/

# actually, no need, since it doesn't seem to cache already-local files
#rm -f ../yum-cache/  
